/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package serversocket;

import com.thoughtworks.xstream.XStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 * Clase para iniciar el servidor
 * 
 * @author Carlos Alvarez, Jesus Vasquez, Luis Carvajal
 */
public class Main {

    private static Config Configuracion;
    
    /**
     * Main de la clase
     * 
     * @param args linea de comando de argumentos
     */
    public static void main(String[] args) {
        int No = 0;
        ServerConexThread SiguenteNodo;
        ServerConexThread NodoAnterior;
        WelcomeConexThread wctHiloBienvenida;
        
        System.out.println("Server:");

        try{
            Main.cargarConfig();
            System.out.println("Soy el servidor numero "+Main.Configuracion.getServNo());
            SiguenteNodo = new ServerConexThread(Main.Configuracion.getPuertoSig(),2,Main.Configuracion.getIpSig(),Main.Configuracion.getPuertoSig());
            SiguenteNodo.start();
            NodoAnterior = new ServerConexThread(Main.Configuracion.getPuertoAnt(),1," "," ");
            NodoAnterior.start();
            
            wctHiloBienvenida = new WelcomeConexThread();
            wctHiloBienvenida.start();
            
        }catch(Exception e){
            System.out.println(e.getClass());
            System.out.println(e.getMessage());
        }
        
        
    }
    
    /**
     * Carga el archivo de configuración del servidor
     * 
     */
    private static void cargarConfig(){
        try {
            XStream xstream = new XStream();
            xstream.alias("Config", Config.class);
            BufferedReader in;
            String path = "";
            try {
                path = System.getProperty("user.dir")+"/Config.xml";            
                File file = new File(path);
                String a = file.getAbsolutePath();
                in = new BufferedReader(new FileReader(file.getAbsolutePath()));
                String s, s2 = new String();
                try {
                    while ((s = in.readLine()) != null) {
                        s2 += s + "\n";
                    }
                    in.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
               Main.Configuracion = new Config(); 
               Main.Configuracion = (Config) xstream.fromXML(s2);
               
            } catch (FileNotFoundException ex) {
                System.out.println("No se encontro el archivo.");
            }
            

        } catch (Exception e) {
            System.out.println(e.getClass());
            System.out.println(e.getMessage());
        }
    }
}
